import {json} from "@remix-run/node";
import {authenticate} from "../shopify.server";
import axios from "axios";

export const loader = async ({ request }) => {
  // The authenticate.admin method returns a CORS method to automatically wrap responses so that extensions, which are hosted on extensions.shopifycdn.com, can access this route.
  const { cors,admin,session } = await authenticate.admin(request);

  const url = new URL(request.url);
  const syncStatus = url.searchParams.get("syncStatus");
  console.log('后台拿到的同步状态：',syncStatus)

  const response1 = await admin.rest.resources.Order.all({
    session: session,
    status: "any",
  });
  const shopifyOrderList = response1?.data || [];

  const response2 = await axios.get('http://127.0.0.1:8123/api/shopify/queryOrderSyncList',{
    params:{
      syncStatus
    }
  });
  const resDataList = response2.data.data || [];


  resDataList.forEach(bizOrderItem => {
    shopifyOrderList.forEach(shopifyOrderItem => {
      if (bizOrderItem.id == shopifyOrderItem.id) {
        bizOrderItem.oriOrder = shopifyOrderItem;
      }
    })
  })


  return cors(json(resDataList));
};
